package org.amj.srt;

import java.text.DecimalFormat;
import java.util.Arrays;

public class SrtTimeEntry {
	private int hour;
	private int minute;
	private int second;
	private int milli;
	private SrtEntry ertEntry;

	public SrtTimeEntry(SrtEntry ertEntry) {
		this.ertEntry = ertEntry;
	}

	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	public int getMinute() {
		return minute;
	}
	public void setMinute(int minute) {
		this.minute = minute;
	}
	public int getSecond() {
		return second;
	}
	public void setSecond(int second) {
		this.second = second;
	}
	public int getMilli() {
		return milli;
	}
	public void setMilli(int milli) {
		this.milli = milli;
	}

	public String toSrtEntry() {
		return format(hour, 2) + ":" + format(minute, 2) + ":" + format(second, 2) + "," + format(milli, 3);
	}

	private String format(int time, int length) {

		char[] zeros = new char[length];
		Arrays.fill(zeros, '0');

		DecimalFormat df = new DecimalFormat(String.valueOf(zeros));

		return df.format(time);
	}

	public void setAll(int h, int m, int s, int mi) {
		setHour(h);
		setMinute(m);
		setSecond(s);
		setMilli(mi);
		
	}

	public void shift(int seconds) {
		int t = seconds + getTotalSecond();
		setAll(0, 0, 0, 0);
		int r = t % 60;
		setSecond(r);
		
		t = t - r;
		
		if(t > 0) {
			t = (t / 60);
			r = t % 60;
			setMinute(r);
			
			t = t - r;
			
			if(t > 0) {
				setHour((t / 60));
			}
		}
	}

	public int getTotalSecond() {
		return second + (minute * 60) + (hour * 60 * 60);
	}
	
	public void setTotalSecond(int seconds) {
		setAll(0, 0, 0, 0);
		shift(seconds);
	}

	@Override
	public String toString() {
		return "SrtTimeEntry [hour=" + hour + ", minute=" + minute
				+ ", second=" + second + ", milli=" + milli + "]";
	}


	
	
}
